function output = lolik_indiv(pm2,dt,rn)

% Construct the negative log-likelihood function which is used in the ML
% estimation of the logit model. 

% pm2 is a 6x1 vector of parameters, pm2 = (alpha,beta,kappa,delta,gamma,lambda),
% or a 7x1 vector of parameters if rn=2 pm2 = (alpha,beta,kappa,delta,gamma,lambda,rho) 
% dt contains the data
% rn indicates whether or not risk neutrality is assumed (rn=0 uses the 
% lottery choices for rho, rn=1 implies risk neutrality and rn=2 
% estimates rho as free parameter)

lot = dt(1,10);     % lottery chosen   
id = dt(1,11);      % id number (session#*100+subjectid)
    
risky2=[4.71 2.95 1.19 0.77 0.32 -0.13];    % rho parameters from lotteries in the first session
risky=[1.61 1 0.39 0.25 0.08 -0.09];        % rho parameters from lotteries in all other sessions

alpha = pm2(1); beta = pm2(2); kappa = pm2(3); delta=pm2(4); gamma=pm2(5); lambda=pm2(6); 
    
if rn==2
    rho=pm2(7);
elseif rn==0
    if id<200
        rho=risky2(lot);
    end
    if id>=200
        rho=risky(lot);
    end
elseif rn==3
    rho=1;
else
    rho=0;
end
    
[n,m] = size(dt); lo_lik = 0; 

eu=zeros(1,8);
eu2=zeros(1,4);
eu3=zeros(1,4);

for i = 1:n
    
    theta = dt(i,1:4);  % payoff parameters
    prefs = [alpha beta kappa delta gamma]; % preference parameters
    y = dt(i,5:7);      % beliefs 
    choice = dt(i,8);   % strategy 
    game = dt(i,9);     % 1=spd or 2=ug or 3=tg
     
    if game==1
        for str = 1:8
            eu(str) = eu_spd(prefs,rho,theta,str,y);
        end
        lo_lik = lo_lik + (1/lambda)*eu(choice)-log(sum(exp((1/lambda)*eu)));
    end

    if game==2
        for str = 1:4
            eu2(str) = eu_ug(prefs,rho,theta,str,y);
        end
        lo_lik = lo_lik + (1/lambda)*eu2(choice)-log(sum(exp((1/lambda)*eu2)));    
    end
    
    if game==3
        for str = 1:4
            eu3(str) = eu_tg(prefs,rho,theta,str,y);
        end
        lo_lik = lo_lik + (1/lambda)*eu3(choice)-log(sum(exp((1/lambda)*eu3)));     
    end
    
end

% output is negative log-likelihood function with n observations
output = - lo_lik;